package com.cheer.ba.view.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cheer.ba.R;
import com.cheer.ba.model.MyOrderModel;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.ImageUtils;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.TextViewUtils;

import java.util.List;

/**
 * 处理我的订单adp
 * Created by Hq on 2017/7/3.
 */
public class MyOrderAdp extends BaseQuickAdapter<MyOrderModel, BaseViewHolder> {

    public MyOrderAdp(@LayoutRes int layoutResId, @Nullable List<MyOrderModel> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, MyOrderModel item) {
        if (null != item) {
            //*************************************柴油批发*************************************/
            View mOilSend = helper.getView(R.id.id_layout_oil_send);
            //支付定金
            TextView mTxtNeedPay = helper.getView(R.id.id_pay_less_txt);
            //剩余支付
            TextView mTxtAllPay = helper.getView(R.id.id_pay_more_txt);
            //取消订单
            Button mBtnCancelOrder = helper.getView(R.id.id_cancel_order_btn);
            //按钮1
            Button mBtn1 = helper.getView(R.id.id_ask_back_money_btn);
            mBtn1.setVisibility(View.VISIBLE);
            //按钮2
            Button mBtn2 = helper.getView(R.id.id_call_kefu_btn);
            mBtn2.setVisibility(View.VISIBLE);
            //*************************************柴油充值*************************************/
            View mOilRecharge = helper.getView(R.id.id_layout_oil_recharge);
            //支付状态
            ImageView mImgPayStatus = helper.getView(R.id.id_order_type_img);
            //是否支付
            LinearLayout mLinearPayStatus = helper.getView(R.id.id_pay_status_linear);
            //是否支付完成
            TextView mTxtRemind = helper.getView(R.id.id_remind_txt);

            //*************************************数据处理*************************************/
            //产品　类型 3000柴油卡购买  3001 汽油  3003 柴油卡激活 3004柴油批发定金
            String productType = item.getProductType();
            if (TextUtils.equals("3000", productType) || TextUtils.equals("3003", productType)) {
                if (TextUtils.equals("3000", productType)) {
                    String msg = "类型：柴油卡充值";
                    TextViewUtils.setSpannableStyleColor(msg, 0, 3, (TextView) helper.getView(R.id.id_oil_order_type_txt), R.color.color_333333);
                } else {
                    String msg = "类型：柴油卡激活";
                    TextViewUtils.setSpannableStyleColor(msg, 0, 3, (TextView) helper.getView(R.id.id_oil_order_type_txt), R.color.color_333333);
                }
                //创建时间
                helper.setText(R.id.id_order_time_txt, StringUtils.getDetailStr1(item.getCreateTime() + ""));
                //标题
                String desc = item.getOrderDesc() + "  " + Currency.returnDollar(item.getCost()) + "  " + item.getProductDesc() + "  " + item.getStartDesc();
                helper.setText(R.id.id_oil_desc_txt, desc);
                //实际付金额
                helper.setText(R.id.id_oil_pay_price_txt, Currency.returnDollar(item.getCostTrue()));
                //订单编号
                helper.setText(R.id.id_order_num_txt, item.getOrderId());

                //柴油充值的或者激活的
                mOilSend.setVisibility(View.GONE);
                mOilRecharge.setVisibility(View.VISIBLE);

                //-1全部 1 待支付 4：已支付5已完成
                int orderState = item.getOrderState();
                if (orderState == 1) {
                    //待支付
                    mLinearPayStatus.setVisibility(View.VISIBLE);
                    mImgPayStatus.setVisibility(View.GONE);
                    mTxtRemind.setVisibility(View.GONE);
                    //需要支付金额
                    String msg = "待支付：" + Currency.returnDollar(item.getCostTrue());
                    TextViewUtils.setSpannableStyleColor(msg, 0, 4, (TextView) helper.getView(R.id.id_need_pay_txt), R.color.color_333333);
                    //取消订单
                    helper.addOnClickListener(R.id.id_oil_recharge_cancel_btn);
                    //支付
                    helper.addOnClickListener(R.id.id_oil_recharge_pay_btn);
                } else if (orderState == 4) {
                    //已支付
                    mLinearPayStatus.setVisibility(View.GONE);
                    mImgPayStatus.setVisibility(View.VISIBLE);
                    mImgPayStatus.setBackgroundResource(R.drawable.ic_already_paid);
                    mTxtRemind.setVisibility(View.GONE);
                } else if (orderState == 5) {
                    //已完成
                    mLinearPayStatus.setVisibility(View.GONE);
                    mImgPayStatus.setVisibility(View.VISIBLE);
                    mImgPayStatus.setBackgroundResource(R.drawable.ic_completed);
                    mTxtRemind.setVisibility(View.VISIBLE);
                }
            } else if (TextUtils.equals("3004", productType)) {
                //柴油批发的
                mOilSend.setVisibility(View.VISIBLE);
                mOilRecharge.setVisibility(View.GONE);
                //公司名称
                helper.setText(R.id.id_oil_company_txt, item.orgNickName);
                helper.addOnClickListener(R.id.id_oil_company_txt);
                //进入商品详情
                helper.addOnClickListener(R.id.id_jump_detail_linear);
                //创建时间
                helper.setText(R.id.id_oil_time_txt, StringUtils.getDetailStr1(item.getCreateTime() + ""));
                ImageUtils.showRoundImg(mContext, Protect.setPic(item.orgLogo), R.drawable.ic_bg_oil_sale, R.drawable.ic_bg_oil_sale, (ImageView) helper.getView(R.id.id_oil_pic));
                helper.setText(R.id.id_oil_title_txt, "柴油 0# " + (TextUtils.equals("1", item.oilModel) ? "普柴" : "国V"));
                helper.setText(R.id.id_oil_density_txt, "密度：" + item.oilDensity);
                helper.setText(R.id.id_oil_type_txt, item.oilSourceName);
                //单价
                helper.setText(R.id.id_oil_price_txt, Currency.returnDollar(item.tonPrice) + "/吨");
                //购买数量
                helper.setText(R.id.id_oil_num_txt, "x " + item.stock);

                //-1全部 1 待支付 4：已支付5已完成 7申请退款
                int orderState = item.getOrderState();
                if (orderState == 1) {
                    //待支付
                    TextViewUtils.setSpannableStyleColor("待支付：" + Currency.returnDollar(item.getCostTrue()), 0, 4, mTxtNeedPay, R.color.color_333333);
                    double pay = Double.parseDouble(item.getCost()) - Double.parseDouble(item.getCostTrue());
                    TextViewUtils.setSpannableStyleColor("线下需付尾款：" + Currency.returnDollar(pay + ""), 0, 7, mTxtAllPay, R.color.color_333333);
                    //取消订单
                    mBtnCancelOrder.setVisibility(View.VISIBLE);
                    mBtnCancelOrder.setText("取消订单");
                    helper.addOnClickListener(R.id.id_cancel_order_btn);
                    mBtn1.setVisibility(View.GONE);
                    mBtn2.setText("联系客服");
                    helper.addOnClickListener(R.id.id_call_kefu_btn);
                } else if (orderState == 4 || orderState == 7) {
                    //已支付
                    TextViewUtils.setSpannableStyleColor("已支付：" + Currency.returnDollar(item.getCostTrue()), 0, 4, mTxtNeedPay, R.color.color_333333);
                    double pay = Double.parseDouble(item.getCost()) - Double.parseDouble(item.getCostTrue());
                    TextViewUtils.setSpannableStyleColor("线下需付尾款：" + Currency.returnDollar(pay + ""), 0, 7, mTxtAllPay, R.color.color_333333);
                    //查看详情
                    mBtnCancelOrder.setVisibility(View.VISIBLE);
                    mBtnCancelOrder.setText("查看详情");
                    helper.addOnClickListener(R.id.id_cancel_order_btn);
                    if (orderState == 7) {
                        mBtn1.setText("已申请退款");
                    } else {
                        mBtn1.setText("申请退款");
                    }
                    helper.addOnClickListener(R.id.id_ask_back_money_btn);
                    mBtn2.setText("联系客服");
                    helper.addOnClickListener(R.id.id_call_kefu_btn);
                } else if (orderState == 5) {
                    //已完成
                    mTxtNeedPay.setVisibility(View.GONE);
                    TextViewUtils.setSpannableStyleColor("已支付总额：" + Currency.returnDollar(item.getCost()), 0, 6, mTxtAllPay, R.color.color_333333);
                    mBtnCancelOrder.setVisibility(View.GONE);
                    mBtn1.setText("查看详情");
                    helper.addOnClickListener(R.id.id_ask_back_money_btn);
                    mBtn2.setText("去评价");
                    helper.addOnClickListener(R.id.id_call_kefu_btn);
                }
            }
        }
    }
}
